********************************************************************************************************
*************** Multilevel regressions: Individual heterogeneity (Appendix C.9) ************************
********************************************************************************************************
clear
use "BHPS_merged.dta"

do "ML programs to add model statistics.do"			

set scheme plottig

* global with individual-level control variables
	global controls "i.education i.male c.age_cent##c.age_cent i.bornelsewhere2 i.parentsbornelsewhere"
	

******************************************* Moderator: Current occupation status 
	global moderator "jbstat2"

	*************** Regression: EU membership support
	global tradevar "real_gr_imp_CH_d_eu_nl"		
		
	mixed D_EUsupport c.LDV_EUsupport##i.year ///
			c.$tradevar##i.$moderator ///
			emplsh_manufact_1998 ///
			$controls i.ID_NUTS1xYear  ///
			if (year==2006 | year==2002) ///
			|| ID_NUTS3xYear: R.$moderator, stddeviations

	margins, dydx($tradevar) at($moderator=(0/5)) post
			eststo marg_EU_gr
				
	*************** Regression: Nationalist attitudes
	global tradevar "real_gr_imp_CH_d3_nl"		

	mixed D3_nationalism L3.nationalism ///
		c.$tradevar##i.$moderator ///
		emplsh_manufact_1998 ///
		$controls i.ID_NUTS1xYear  ///
		if (year==2008 | year==2005 | year==2002) ///
		|| ID_NUTS3xYear: R.$moderator, stddeviations
		
	margins, dydx($tradevar) at($moderator=(0/5)) post
		eststo marg_nat_gr
				
	*** combine to one graph		
	coefplot marg_EU_gr || marg_nat_gr, ///
		 coeflabels(1._at = "employee" 2._at = "self-employed" 3._at = "unemployed" 4._at = "retired" 5._at = "student" 6._at = "other inactive") ///
		 xline(0) level(95 85)  ///
		 byopts(rows(1) title(Moderator: current labor force status)) bylabels("EU membership" "Nationalist attitude" ) ///
		name(Modgr_jobstatus, replace) nodraw
			 

******************************************************* Moderator: Education 
	global moderator "education"
		
	*************** Regression: EU membership support
	global tradevar "real_gr_imp_CH_d_eu_nl"		
		
	mixed D_EUsupport c.LDV_EUsupport##i.year ///
			c.$tradevar##i.$moderator ///
			emplsh_manufact_1998 ///
			$controls i.ID_NUTS1xYear  ///
			if (year==2006 | year==2002) ///
			|| ID_NUTS3xYear: R.$moderator, stddeviation					

	margins, dydx($tradevar) at($moderator=(0/5)) post
			eststo marg_EU_gr
				
	*************** Regression: Nationalist attitudes
	global tradevar "real_gr_imp_CH_d3_nl"		

	mixed D3_nationalism L3.nationalism ///
		c.$tradevar##i.$moderator ///
		emplsh_manufact_1998 ///
		$controls i.ID_NUTS1xYear  ///
		if (year==2008 | year==2005 | year==2002) ///
		|| ID_NUTS3xYear: R.$moderator, stddeviations	

	margins, dydx($tradevar) at($moderator=(0/5)) post
		eststo marg_nat_gr		
		
	*** combine to one graph		
	coefplot marg_EU_gr || marg_nat_gr, ///
		 coeflabels(1._at = "No qualification" 2._at = "Other qualification" 3._at = "GCSE etc" 4._at = "A-level etc" 5._at = "Other higher degree" 6._at = "Degree") ///
		 xline(0) level(95 85)  ///
		 byopts(rows(1) title("Moderator: education")) bylabels("EU membership" "Nationalist attitude" ) ///
		 name(Modgr_education, replace) nodraw


******************************************************* Moderator: Immigration background 
	capture gen migrant=0 if bornelsewhere2==0 & parentsbornelsewhere==0
	replace migrant=1 if bornelsewhere2==1 | parentsbornelsewhere==1 | parentsbornelsewhere==2
	
	global moderator "migrant"
		
	*************** Regression: EU membership support
	global tradevar "real_gr_imp_CH_d_eu_nl"		
		
	mixed D_EUsupport c.LDV_EUsupport##i.year ///
			c.$tradevar##i.$moderator ///
			emplsh_manufact_1998 ///
			$controls i.ID_NUTS1xYear  ///
			if (year==2006 | year==2002) ///
			|| ID_NUTS3xYear: R.$moderator, stddeviations					

	margins, dydx($tradevar) at($moderator=(0/1)) post
			eststo marg_EU_gr
				
	*************** Regression: Nationalist attitudes
	global tradevar "real_gr_imp_CH_d3_nl"		

	mixed D3_nationalism L3.nationalism ///
		c.$tradevar##i.$moderator ///
		emplsh_manufact_1998 ///
		$controls i.ID_NUTS1xYear  ///
		if (year==2008 | year==2005 | year==2002) ///
		|| ID_NUTS3xYear: R.$moderator, stddeviations		

	margins, dydx($tradevar) at($moderator=(0/1)) post
		eststo marg_nat_gr		

	*** combine to one graph		
	coefplot marg_EU_gr || marg_nat_gr, ///
		 coeflabels(1._at = "No immigration" 2._at = "Immigration") ///
		 xline(0) level(95 85)  ///
		 byopts(rows(1) title("Moderator: immigration")) bylabels("EU membership" "Nationalist attitude" ) ///
		 name(Modgr_immigration, replace) nodraw


******************************************************* Moderator: Sector of employment 
	capture recode jbsic92_section (1 2 3=1) (4=2) (5/17=3), gen(gensector)
	capture replace gensector=0 if gensector==.

	global moderator "gensector"

	*************** Regression: EU membership support
	global tradevar "real_gr_imp_CH_d_eu_nl"		
		
	mixed D_EUsupport c.LDV_EUsupport##i.year ///
			c.$tradevar##i.$moderator ///
			emplsh_manufact_1998 ///
			$controls i.ID_NUTS1xYear  ///
			if (year==2006 | year==2002) ///
			|| ID_NUTS3xYear: R.$moderator, stddeviations

	margins, dydx($tradevar) at($moderator=(0/3)) post
			eststo marg_EU_gr
			
	*************** Regression: Nationalist attitudes
	global tradevar "real_gr_imp_CH_d3_nl"		

	mixed D3_nationalism L3.nationalism ///
		c.$tradevar##i.$moderator ///
		emplsh_manufact_1998 ///
		$controls i.ID_NUTS1xYear  ///
		if (year==2008 | year==2005 | year==2002) ///
		|| ID_NUTS3xYear: R.$moderator, stddeviations 
		
	margins, dydx($tradevar) at($moderator=(0/3)) post
		eststo marg_nat_gr		
	
*** combine to one graph		
	coefplot marg_EU_gr || marg_nat_gr, ///
		 coeflabels(1._at = "Sector missing" 2._at = "Primary sector" 3._at = "Manufacturing" 4._at = "Services" ) ///
		 xline(0) level(95 85)  ///
		 byopts(rows(1) title("Moderator: sector of employment")) bylabels("EU membership" "Nationalist attitude" ) ///
		 name(Modgr_sector, replace) nodraw	 
		
		
		
******************************************************* combine to one graph		
		graph combine Modgr_jobstatus Modgr_immigration Modgr_education Modgr_sector, imargin(small) iscale(*1.1) name(Moderators, replace)
		
		
